/**
 * Created by lyf on 2017/6/20.
 */


'use strict'
let node_ssh = require('node-ssh');
const serviceConfig = require('../conf/service.json');
let ssh = new node_ssh();

module.exports = function deployAccesspoint(accesspoint , zookeeper , redis, elasticsearch, tsungMaster) {
    return new Promise (async function(resolve,reject){
        console.log(accesspoint);
        for ( let i = 0 ; i < accesspoint.length ; i++){
            await ssh.connect({
                host: accesspoint[i][1],
                username: serviceConfig.user ,
                password: serviceConfig.password
            });

            let result = await ssh.execCommand( `docker run -d` +
                ` --log-driver=fluentd --log-opt "fluentd-address=$(ifconfig eth0 | grep inet | awk '{print $2}'):24224"` +
                ` -e TZ="Asia/Shanghai" -v /etc/localtime:/etc/localtime:ro ` +
                ` -e SK_KAFKA_ZOOKEEPER="${zookeeper[0][0]}:2181"` +
                ` -e SK_REDIS="redis://:${serviceConfig.password}@${redis[0][0]}" ` +
                ` -e SERVER_IP="$(ifconfig eth0 | grep inet | awk '{print $2}')" ` +
                ` -e ZOOKEEPER_ADDR="${zookeeper[0][0]}:2181" ` +
                ` --net=host` +
                ` --name accesspoint-service   foreveross/accesspoint-service`
            );
            //result.stdout ? resolve() : reject ();

            console.log(`accesspoint:${accesspoint[i][1]}`);
            console.log(`zookeeper:${zookeeper[0][0]}`);
            console.log("-----------------------------");
            console.log(result.stdout);
            console.log(result.stderr);
            console.log("-----------------------------");
            // start
            await ssh.execCommand (`/root/test/ab-tsung/masterSlaveMode/start_target.sh ${tsungMaster[0][0]}`);
            ssh.dispose();
            resolve();

        }
    })

}